///////////////////////////////////////////////////////////////////////////////
///// Assignment 3: CPU Scheduler
///// By: Brady Robichaud (robicb82) & Patrick Filtz (filtzp43)
///// Date: 4/19/2013
///// File: FCFS.java
///// Description: This class deals with the first come first serve scheduler.
/////	
///////////////////////////////////////////////////////////////////////////////
package round2;

public class FCFS extends Queue{
	public FCFS(Process[] ps){
		this.ps = ps;
		
		for(int i = 0; i < ps.length; i++){
			psHash.put(ps[i].pID, ps[i]);
		}
		cpuRoot = cpuHead = new Node(false);
		cpuTail = cpuHead;

		ioRoot = ioHead = new Node(true);
		ioTail = ioHead;
		title = "_____________First Come First Serve_____________";
	}
	
	public String run(){
		Process p;
		for(int time = 0; anyActive() ; time++){//time iteration loop
			for(int i = 0; i < ps.length; i++){//for each process
				p = ps[i];
				if(p.hasMore() && p.hasArrived(time) && p.burstReady){//if next burst is ready
					//add next burst to queue
					if(p.curIndex%2 == 0){
						addCPU(p);
						setProcReady(p.pID, false);
					}else{
						addIO(p);
						setProcReady(p.pID, false);
					}
				}
			}
			incrementWaits(cpuHead);
			incrementWaits(ioHead);
			
			
			if(cpuHead.waitNode){//Checks if waitNode
				idleTime++;
			}else {
				cpuHead.curRemTime--;
				if(cpuHead.curRemTime <= 0){ //Remaining time is less than 0
					setProcReady(cpuHead.pID, true);
					psHash.get(cpuHead.pID).burstDone(time);
					psHash.get(cpuHead.pID).curIndex++;
					cpuHead = cpuHead.nextNode;
				}
			}
			if(!ioHead.waitNode){
				ioHead.curRemTime--;
				if(ioHead.curRemTime <= 0){
					setProcReady(ioHead.pID, true);
					psHash.get(ioHead.pID).burstDone(time);
					psHash.get(ioHead.pID).curIndex++;
					ioHead = ioHead.nextNode;
				}
			}
		}
		return getSummary(); //Prints strings and values to GUI
	}
			
	//Adds processes to the CPU and links the nodes together
	private void addCPU(Process p){
		Node newN = new Node(p.pID, p.bursts[p.curIndex], p.bursts[p.curIndex], false);
		if(cpuHead.waitNode){
			cpuHead.nextNode = newN;
			cpuHead = cpuHead.nextNode;
		}else{
			Node curNode = cpuHead;
			while(!curNode.nextNode.waitNode){
				curNode = curNode.nextNode;
			}
			curNode.nextNode = newN;
		}
	}	
}